/*****************************************************************
Mix3D WebGL Game Engine
Copyright (c) fantasy ( fantasy.x2x@gmail.com )
Listens:
Distributed under the Boost Software License, Version 1.0.
http://www.boost.org/LICENSE_1_0.txt
 ****************************************************************/
/**
 * @Desc    : frusum 
 * @Author  : fantasy(fantasy.x2x@gmail.com)
 */
//////////////////////////////////////////////////////////////////
if(typeof(Mix3D) === 'undefined')
{
  Mix3D = {};
};
(function(Mix3D){
//////////////////////////////////////////////////////////////////
Mix3D.Frustum = function()
{
  this.plans = 
  [
    new Mix3D.Vector4(),
    new Mix3D.Vector4(),
    new Mix3D.Vector4(),
    new Mix3D.Vector4(),
    new Mix3D.Vector4(),
    new Mix3D.Vector4()
  ];
};

Mix3D.Frustum.prototype.constructor = Mix3D.Frustum;

Mix3D.Frustum.prototype.fromMatrix = function(matrix)
{
  var i, plane, planes = this.planes;
  var m = matrix.FA;

	planes[ 0 ].set( m[3] - m[0], m[7] - m[4], m[11] - m[8], m[15] - m[12] );
	planes[ 1 ].set( m[3] + m[0], m[7] + m[4], m[11] + m[8], m[15] + m[12] );
	planes[ 2 ].set( m[3] + m[1], m[7] + m[5], m[11] + m[9], m[15] + m[13] );
	planes[ 3 ].set( m[3] - m[1], m[7] - m[5], m[11] - m[9], m[15] - m[13] );
	planes[ 4 ].set( m[3] - m[2], m[7] - m[6], m[11] - m[10], m[15] - m[14] );
	planes[ 5 ].set( m[3] + m[2], m[7] + m[6], m[11] + m[10], m[15] + m[14] );

	for ( i = 0; i < 6; ++i) 
	{
		plane = planes[ i ];
		plane.divScalar( Math.sqrt( plane.FA[0] * plane.FA[0] + plane.FA[1] * plane.FA[1] + plane.FA[2] * plane.FA[2] ) );
	}
};
//////////////////////////////////////////////////////////////////
})(Mix3D);// end of function(Mix3D)